PlayerMenuVisible = false;
RadioMenuVisible = false;

PlayerMenuIntro = 0;

function event.PlayerMenuOff()

	if( PlayerMenuVisible ) then
	
		event.TogglePlayerMenu();
	
	end

end

function event.TogglePlayerMenu()

	PlayerMenuVisible = !PlayerMenuVisible;
	
	StorageItemID = nil;
	
	if( MEPropertiesWindow and MEPropertiesWindow:IsValid() ) then
	
		MEPropertiesWindow:SetVisible( PlayerMenuVisible );
		MEPropertiesWindow.Menu:SetVisible( PlayerMenuVisible );
		
		if( MEPropertiesWindow.TitleBar and MEPropertiesWindow.TitleBar:IsValid() ) then	
			MEPropertiesWindow.TitleBar:SetVisible( PlayerMenuVisible );
			MEPropertiesWindow.Menu.TitleBar:SetVisible( PlayerMenuVisible );
		end
		
	end

	for k, v in pairs( PlayerMenuLinks ) do

		v:SetVisible( PlayerMenuVisible );
		
		local x, y = v:GetPos();
		
		v:SetPos( -1000, y );

	end
	
	if( PlayerInfoPanel and PlayerInfoPanel:IsValid() ) then
	
		if( PlayerInfoPanel.TitleBar and PlayerInfoPanel.TitleBar:IsValid() ) then
			PlayerInfoPanel.TitleBar:SetVisible( PlayerMenuVisible );
		end
		
		PlayerInfoPanel:SetVisible( PlayerMenuVisible );
	
	end
	
	
	if( PlayerMenuVisible ) then
	
		gui.EnableScreenClicker( true );
		
		--PlayerMenuIDCard:SetVisible( true );
		
		InventoryMenu:SetVisible( true );
		InventoryMenu.TitleBar:SetVisible( true );
		
		PlayerMenuIntro = 0;
		
		RemoveItemButtons();
		
	else
	
		if( StorageMenu ) then
			StorageMenu:Remove();
			StorageMenu = nil;
		end
	
		--PlayerMenuIDCard:SetVisible( false );
		
		InventoryMenu:SetVisible( false );
		InventoryMenu.TitleBar:SetVisible( false );
		
		HideMouse();
	
	
	end
	
end


surface.CreateFont( "TargetID", 22, 800, true, false, "IDHeader" );
surface.CreateFont( "TargetID", 18, 800, true, false, "IDHeader2" );
surface.CreateFont( "BudgetLabel", 16, 200, true, false, "IDInfo" );
surface.CreateFont( "BudgetLabel", 16, 800, true, false, "IDInfo2" );
surface.CreateFont( "BudgetLabel", 9, 100, true, false, "IDInfo3" );
surface.CreateFont( "ParmaPetit", 20, 1200, true, false, "PlayerMenuFactionFont" );


function DrawPlayerMenu()

	for k, v in pairs( PlayerMenuLinks ) do
	
		local x, y = v:GetPos();
	
		v:SetPos( ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ), y );
	
	end

	local sw = 125;
	
	HealthAlpha = 255;
	SprintAlpha = 255;
	
	local plur = "s";
	
	if( ClientVars["Money"] == 1 ) then
		plur = "";
	end
	
	--draw.DrawText( HL2Factions[ClientVars["Faction"]].Name, "PlayerMenuFactionFont", ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ) + 2, ScrH() - sw - 40, Color( 255, 255, 255, 255 ) );
	draw.DrawText( "You have " .. ClientVars["Money"] .. " credit" .. plur .. "\n" .. "You owe " .. ClientVars["Loans"] .. " credits to the union\n", "NewChatFont", ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ) + 2, ScrH() - sw - 10, Color( 255, 255, 255, 255 ) );

end

function PlayerMenuThink()

	if( PlayerMenuIntro < 100 ) then
		
		PlayerMenuIntro = math.Clamp( PlayerMenuIntro + 500 * FrameTime(), 0, 100 );
	
	end

end

function CreateIDCard()

	PlayerMenuIDCard = CreateBPanel( nil, 10, 10, 300, 200 );
	PlayerMenuIDCard:CanClose( true )
	PlayerMenuIDCard:CanSeeTitle( true )

	PlayerMenuIDCard.OnMousePressed = function( self )
	
		self.Dragging = true;
		self.DragXOffset, self.DragYOffset = gui.MousePos();
		
		local x, y = self:GetPos();
		
		self.DragXOffset = self.DragXOffset - x;
		self.DragYOffset = self.DragYOffset - y;
		
	end
	
	PlayerMenuIDCard.OnMouseReleased = function( self )
	
		self.Dragging = false;
	
	end
	
	PlayerMenuIDCard.Think = function( self )

		if( not input.IsMouseDown( MOUSE_LEFT ) ) then
		
			self.Dragging = false;
		
		end
	
		if( self.Dragging ) then
	
			local mx, my = gui.MousePos();
			
			mx = mx - self.DragXOffset;
			my = my - self.DragYOffset;
		
			self:SetPos( mx, my );

		end
	
	end
	
	PlayerMenuIDCard.IDImage = vgui.Create( "DModelPanel", PlayerMenuIDCard );

	PlayerMenuIDCard.IDImage:SetPos( 15, 50 );
	PlayerMenuIDCard.IDImage:SetSize( 84, 84 );
	PlayerMenuIDCard.IDImage:SetFOV( 30 );
	
	PlayerMenuIDCard.IDImage:SetLookAt( Vector( 0, 0, 62 ) );
 	PlayerMenuIDCard.IDImage:SetCamPos( Vector(45, -5, 55 ) );
	
	PlayerMenuIDCard.IDImage.LayoutEntity = function()
	    
 	end
 	
 	PlayerMenuIDCard.IDImage.Think = function()
	
		if( PlayerMenuIDCard.IDImage.Model ~= LocalPlayer():GetModel() ) then
		
			PlayerMenuIDCard.IDImage.Model = LocalPlayer():GetModel();
			PlayerMenuIDCard.IDImage:SetModel( LocalPlayer():GetModel() );
		
		end
	
	end
	
	PlayerMenuIDCard.Paint = function( self )
	
		draw.RoundedBox( 8, 0, 0, self:GetWide(), self:GetTall(), Color( 252, 251, 225, 255 ) );
		draw.RoundedBox( 8, 4, 4, self:GetWide() - 8, self:GetTall() - 8, Color( 236, 236, 221, 255 ) );
	 	 	
	 	local x, y = PlayerMenuIDCard.IDImage:GetPos();
	 	draw.RoundedBox( 0, x, y, PlayerMenuIDCard.IDImage:GetWide(), PlayerMenuIDCard.IDImage:GetTall(), Color( 200, 200, 200, 255 ) );
   
   		draw.DrawText( "Universal Union Acquisition X18", "IDHeader", self:GetWide() / 2, 3, Color( 0, 0, 0, 255 ), 1 );
   		draw.DrawText( "Proof of Naturalised Citizenship", "IDHeader2", self:GetWide() / 2, 25, Color( 0, 0, 0, 255 ), 1 );
	 	
	 	local gender = "Male";
	 	
	 	if( string.find( string.lower( LocalPlayer():GetModel() ), "female" ) ) then
	 	
	 		gender = "Female";
	 	
	 	end
	 	
	 	draw.DrawText( LocalPlayer():GetRPName(), "IDInfo", self:GetWide() - 5, 55, Color( 0, 0, 0, 255 ), 2 );
	 	draw.DrawText( gender, "IDInfo", self:GetWide() - 5, 71, Color( 0, 0, 0, 255 ), 2 );
	 	draw.DrawText( "Age: " .. ClientVars["Age"], "IDInfo", self:GetWide() - 5, 86, Color( 0, 0, 0, 255 ), 2 );
	 	draw.DrawText( "CID Code: " .. LocalPlayer():GetNWFloat( "CID" ), "IDInfo", self:GetWide() - 5, 102, Color( 0, 0, 0, 255 ), 2 );
	 	draw.DrawText( "STATUS: " .. ClientVars["ClassStatus"] .. "\nCLASS: " .. ClientVars["ClassMark"] .. "\nRESTRICTIONS: ***", "IDInfo2", 8, 148, Color( 0, 0, 0, 255 ) );
		draw.DrawText( "If you are in possession of a Citizenship\nIdentification Card, you are subject to the\nUniversal Union codes of conduct, including\npossible cross-examination of citizenship\ndetails by the local Civil Authority enforcement\n teams. This includes recession of therights and\nfreedoms previously documented in classified or\nunauthorised Acquisition X17 texts.", "IDInfo3", self:GetWide() - 3, 120, Color( 0, 0, 0, 255 ), 2 );
		
		surface.SetMaterial( Material( "stampy" ) );
		surface.SetDrawColor( 255, 255, 255, 80 );
		surface.DrawTexturedRect( self:GetWide() - 70, self:GetTall() - 70, 64, 64 );
	end

end
usermessage.Hook( "CID", CreateIDCard )

function CombineRations()

	if( !LocalPlayer():IsCombineDefense() ) then
		CreateOkPanel( "Can't do this", "You need to be combine to be able to do this." );
		return;		
	end	

	RationBox = CreateBPanel( "Rations", 10, 10, 220, 75 )
	
	RationBox:AddLabel( "Drop a ration.", "NewChatFont", 90, 15 )
	
	Model = vgui.Create( "DModelPanel", RationBox );
	Model:SetPos( 5, 5 );
	Model:SetSize( 64, 64 );
	Model:SetModel( "models/weapons/w_package.mdl" );
	Model:SetFOV( 8 );
	Model:SetLookAt( Vector( -99, -23, -200 ) );
	Model:SetCamPos( Vector( 84, 7, 150 ) );
	
	Model.LayoutEntity = function( self ) 
	
	end
	
	local function CreateRation()
	
		LocalPlayer():ConCommand( "rp_makeration" )
		
	end	
	
	RationBox:AddButton( "Drop Ration", 75, 45, function() CreateRation() end )  
	
end	

if( PlayerInfoPanel ) then
	PlayerInfoPanel:Remove();
end
PlayerInfoPanel = nil;

function CreatePlayerInfo()

	PlayerInfoPanel = CreateBPanel( "Player Information", ScrW() / 2 - 200, ScrH() / 2 - 200, 430, 400 );
	
	PlayerInfoPanel.PaintHook = function()
	
		local h, m, s = GetHMS();
		local str;
		
		if( h == 1 ) then
			str = h .. " hour";
		else
			str = h .. " hours";
		end
		
		if( m == 1 ) then
			str = str .. ", " .. m .. " minute";
		else
			str = str .. ", " .. m .. " minutes";
		end
	
		if( s == 1 ) then
			str = str .. ", " .. s .. " second";
		else
			str = str .. ", " .. s .. " seconds";
		end
	
		draw.DrawText( "This session, you've played for " .. str, "NewChatFont", 7, 10, Color( 255, 255, 255, 255 ) );
		draw.DrawText( "You are currently on Character: " .. LocalPlayer():GetRPName(), "NewChatFont", 7, 25, Color( 255, 255, 255, 255 ) );
	
	end

end

if( PlayerMenuLinks ) then
	for k, v in pairs( PlayerMenuLinks ) do
		v:Remove();
	end
end
PlayerMenuLinks = { }

function CreatePlayerMenu()
	
	--CreateIDCard() --not being called?
	
	local colour = Color( 200, 200, 200, 255 )
	
	if( TS.Christmas ) then	
		colour = Color( 255, 0, 0, 255 )	
	end	
	
	local function AddLink( name, y, func )
	
		local link = vgui.Create( "BLink" );
		link:SetPos( ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ), y );
		link:SetFont( "NewChatFont" );
		link:SetText( name );
		link:SizeToContents();
		link.HighlightColor = colour
		link.Action = func;
		
		table.insert( PlayerMenuLinks, link );
		
	end
	
	AddLink( "Open Loan Menu", ScrH() - 290, function() RunConsoleCommand( "eng_olm" ); end )
	AddLink( "Open Ration Menu", ScrH() - 270, function() CombineRations() end )
	AddLink( "Character Create/Select", ScrH() - 250, function() event.TogglePlayerMenu(); RunConsoleCommand( "rp_charcreate" ) end );
	AddLink( "Set Radio Frequency", ScrH() - 230, function () RunConsoleCommand( "eng_promptradiomenu" ) end )
	AddLink( "CID Card", ScrH() - 190, function() RunConsoleCommand( "cid" ) end ) 


end

CreatePlayerMenu();

